#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>


typedef unsigned int uint;
typedef struct sockaddr SA;
typedef unsigned short ushort;

//define const variables
#define LISTENQ 8096
#define DEF_MODE S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH

const int RIO_BUFSIZE=1024;//10*(1<<20);

//define structs
typedef struct {
    int rio_fd;
    int rio_cnt;
    char* rio_bufptr;
    char rio_buf[RIO_BUFSIZE];
} rio_t;

class MySocket {

    public:
        int openClientfd(char* hostname, int port);
        int openListenfd(int port);
};

class MyNetIO {
    private:
        rio_t *rp;
        ssize_t rio_read(char* usrbuf, size_t n);
    public:
        MyNetIO() { rp = new rio_t(); };
        ~MyNetIO() { delete rp; };

        //IO with buffer
        void readinitb(int fd);
        ssize_t readlineb(void* usrbuf, size_t maxlen);
        ssize_t readnb(void* usrbuf, size_t n);
        ssize_t readstring(void* usrbuf, size_t maxlen);
};

ssize_t readn(int fd, void* usrbuf, size_t n);//Read function with no buffer
ssize_t writen(int fd, void* usrbuf, size_t n); //Write function
